package org.otherworld.command;

import java.util.ArrayList;
import java.util.Hashtable;

import org.otherworld.property.PropertyLoader;

public abstract class Command {
	private PropertyLoader loader;
	private Hashtable<String, Object> messages;
	private CommandConst commands;
	
	abstract public String getCommand();
	abstract public ArrayList<String> getConnectors();
	abstract public boolean run(String command) throws Exception;
	
	public final String sep = System.getProperty("file.separator") + System.getProperty("file.separator");

	
	public Command() throws Exception{
		loader = new PropertyLoader();
		messages = loader.getProperties();
		commands = new CommandConst();
	}
	
	public ArrayList<String> getAvailableCommands() {
		return CommandConst.availableCommands;
	}
	
	public ArrayList<String> getAvailableConnectors() {
		return CommandConst.availableConnectors;
	}
	
	/**
	 * Int visszatérési értékkel rendelkező függvény, mely a konnektorok közül visszaadja annak a sorszámát, amelyik megtalálható a
	 * parancsban. Ha nem található meg benne, akkor -1 -et ad vissza, amit a switchben le kell kezelni.
	 * @param connectors ArrayList<String>, mely a connectorokat tartalmazza
	 * @param command maga a parancs
	 * @return int
	 */
	
	public int getConnectorID(ArrayList<String> connectors, String command) {
		String[] commandSplit = command.split(" ");
		int con = -1;
		if (commandSplit.length > 1) {
			String param = commandSplit[1];
			

			if (connectors.contains(param)) {
				int counter = 0;
				boolean isGood = false;
				while (!isGood || counter < connectors.size()) {
					if (connectors.get(counter).equals(param)) {
						con = counter;
						isGood = true;
					}
					++counter;
				}

			}
		}
		return con;
	}
	
	public String getSecondConnector(String command) {
		String[] commandSplit = command.split(" ");
		String ret = "";
		if (commandSplit.length > 2) {
			ret = commandSplit[2];
		}
		return ret;
		
	}
	public int getSecondConnectorID(ArrayList<String> connectors, String command) {
		String[] commandSplit = command.split(" ");
		int con = -1;
		if (commandSplit.length > 2) {
			String param = commandSplit[2];
			

			if (connectors.contains(param)) {
				int counter = 0;
				boolean isGood = false;
				while (!isGood || counter < connectors.size()) {
					if (connectors.get(counter).equals(param)) {
						con = counter;
						isGood = true;
					}
					++counter;
				}

			}
		}
		return con;
	}


	
}
